<?php

include_once (SYSTEM. '/Controller.php');
include_once (APPLICATION. '/Common/Paging.php');
//include_once (MODEL. '/SearchModel.php');
include_once (MODEL. '/SearchRepository.php');
include_once (SYSTEM.'/SearchConfig.php');
include_once (MODEL . '/OfferModel.php');
class SearchController extends Controller
{
    var $options = array
        (
            'hostname' => SOLR_SERVER_HOSTNAME,
            'login'    => SOLR_SERVER_USERNAME,
            'password' => SOLR_SERVER_PASSWORD,
            'port'     => 8983,
        );
    public function DeleteData()
    {
    	$client = new SolrClient($this->options);
    	
    	//This will erase the entire index
    	$client->deleteByQuery("*:*");
    	$client->commit();
    }
    public function ImportData()
    {
        $client = new SolrClient($this->options);

        $doc = new SolrInputDocument();

        $search = new SearchRepository();
        $filter =array();
        $listOffer = $search->ListOffer($filter);
        foreach ($listOffer as $item)
        {   
        	//$client->deleteById($item->ID);
            $doc = new SolrInputDocument();
            $doc->addField('id', $item->ID);
            $doc->addField('name', $item->Name);
            $doc->addField('cat', $item->Location);
            $doc->addField('cat', $item->Description);

            $updateResponse = $client->addDocument($doc);
            $client->commit();
        }
    }
    public function Search()
    {
        $this->ViewData['Title'] = 'Search';
        $params = $this->GetParams();
        $client = new SolrClient($this->options);
        $query = new SolrQuery();
        
        if (isset($params['query'])) $queryParam = $params['query']; 
        else if (isset ($params['submit'])) $queryParam = $params['content']; else
        $queryParam = 'migi';
            
        $query->setQuery($queryParam);
        $query->addField('id');
        $query_response = $client->query($query);

        $response = $query_response->getResponse();
        // tren tinh tong so document
        $search = new SearchRepository();
        
        $currentPage = isset($params['page']) ? $params['page'] : 1;       
        $inactivePageTag = 'id="currentPage"';                		
		$pageUrl = '/Search/Search?query='.$queryParam.'&page=';
        $pageUrllast = '';
        $paging =  new Paging();      
        $paging->PageSet($pageUrl, $response->response->numFound, $currentPage, $inactivePageTag, $pageUrllast);        
        
        $client = new SolrClient($this->options);
        $query = new SolrQuery();
        $query->setQuery($queryParam);
        $query->setStart($paging->start);
        $query->setRows($paging->start+$paging->perPage);
        $query->addField('name')->addField('id');
        $query_response = $client->query($query);

        $response = $query_response->getResponse();
        $listResult = array();
        if ($response->response->docs != null)
        foreach ($response->response->docs as $item)
        {
            $filter = array('_id'=> new MongoId($item['id']));
            $temp = $search->ListOffer($filter);
            if ($temp!= null) $listResult[] = $temp[0];
                        
        }
        
        $this->ViewData['listCat'] = $search->GetListCategory();
        $model['result'] = $listResult;
        $model['paging'] = $paging;
        $this->Render('index', '_Master', $model);
        
    }        
}
?>

